package cn.edu.swu.bc.utils;

import cn.edu.swu.bc.entity.Blog;
import cn.edu.swu.bc.entity.User;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

public class JsonUtils {
    public static String blogToJson(List<Blog> blogs, int pageNum, HttpServletRequest request) throws IOException {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"blogs\": [");

        for (int i = 0; i < blogs.size(); i++) {
            Blog blog = blogs.get(i);
            int id=blog.getID();
            //博客的简介在博客内容的第一行存储，故需要读取博客的文本内容
            String blogIntroduction= FileUtils.getBlogContent(id,request).get(0);
            if (i > 0) sb.append(",");
            sb.append(String.format(
                    //拼接数据
                    "{\"id\": \"%s\", \"blogTitle\": \"%s\",\"author\": \"%s\"," +
                            "\"authorAvatar\":\"%s\",\"pic\":\"%s\",\"uid\":\"%s\","+
                            "\"publishTime\":\"%s\",\"blogIntroduction\":\"%s\"}",
                    blog.getID(), blog.getTitle(), blog.getAuthorname(), blog.getAuthorAvatar()
                    ,blog.getPic(),blog.getUID(),blog.getDatetime(),blogIntroduction
            ));
        }

        sb.append("],");
        //取得的博客数量用于分页显示
        String tmp="\"pageNum\":"+pageNum;
        sb.append(tmp);
        sb.append("}");
        return sb.toString();
    }
    public static String userToJson(User user, String isOthers) throws IOException {
        if (user == null)
            return null;
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"userinfo\": ");
        sb.append(String.format(
                "{\"id\": \"%s\", \"uname\": \"%s\",\"nickname\": \"%s\"," +
                        "\"password\": \"%s\", \"birthday\": \"%s\",\"location\": \"%s\"," +
                        "\"avatar\": \"%s\",\"sex\":\"%s\",\"signature\":\"%s\"},\"isOthers\":\"%s\"",
                user.getID(), user.getUsername(), user.getNikcname(),
                user.getPassword(), user.getBirthday(), user.getLocation(),
                user.getAvatar(), user.getSex(), user.getSignature(),isOthers
        ));
        sb.append("}");
        return sb.toString();
    }
    public static String blogFullTextToJson(List<String> blogFullText,String pics,String author,String title,String time,Boolean isManager) throws IOException {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("\"blog\": [");
        String format="{\"type\": \"%s\", \"content\": \"%s\"}";
        for (int i = 0; i < blogFullText.size(); i++) {
            if (i > 0) sb.append(",");
            if(blogFullText.get(i).contains("picture:"))
                sb.append(String.format(format,"img",blogFullText.get(i).substring(8)));
            else
                sb.append(String.format(format,"p",blogFullText.get(i)));
        }
        sb.append("]");
        sb=appendUtils(sb,"pics",pics);
        sb=appendUtils(sb,"title",title);
        sb=appendUtils(sb,"author",author);
        sb=appendUtils(sb,"time",time);
        sb=appendUtils(sb,"isManager",isManager.toString());
        sb.append("}");
        return sb.toString();
    }
    //头部信息打包成json格式
    public static String headInfoToJson(String nikcname,String avatar,String signature) {
        String headInfo="{\"headInfo\":{\"nickname\":\""+nikcname+ "\", \"avatar\": \""+
                avatar+"\", \"signature\": \""+ signature+"\"}}";
        return headInfo;
    }
    private static StringBuilder appendUtils(StringBuilder sb,String name,String value)
    {
        String appendString=",\""+name+"\":\""+value+"\"";
        sb.append(appendString);
        return  sb;
    }
}
